package com.hb.trade.api.im.chat.service;

import cn.hutool.core.collection.CollUtil;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.core.pojo.project.proj.dto.ProjectBaseInfo;
import com.hb.core.pojo.project.section.dto.BidSectionTenderDTO;
import com.hb.trade.client.center.UserClient;
import com.hb.trade.client.project.SectionClient;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.user.layim.LayImUser;
import org.fzzn.core.model.websocket.SocketMessageEntity;
import org.fzzn.core.websocket.service.ImChatService;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class TradeImChatService implements ImChatService {

    protected final SectionClient sectionClient;
    protected final UserClient userClient;
    protected final ChatUserService chatUserService;
    protected final ChatRecordService chatRecordService;
    protected final ChatRecordContentService chatRecordContentService;

    public TradeImChatService(SectionClient sectionClient,
                              UserClient userClient,
                              ChatUserService chatUserService,
                              ChatRecordService chatRecordService,
                              ChatRecordContentService chatRecordContentService) {
        this.sectionClient = sectionClient;
        this.userClient = userClient;
        this.chatUserService = chatUserService;
        this.chatRecordService = chatRecordService;
        this.chatRecordContentService = chatRecordContentService;
    }

    /**
     * 记录聊天记录
     * 接收方已经收到的记录
     *
     * @param message 消息正文(layim接收的完整格式)
     * @param toUname 接收人
     */
    @Override
    public void record(SocketMessageEntity message, String toUname) {
        record(message, toUname, false);
    }

    /**
     * 记录消息
     * 该方法记录的聊天消息只为支持离线消息使用
     * 聊天记录的查询需要查询im_chat_record_content表
     * 该方法被调用时会操作im_chat_record_content表进行保存
     * 该方法未调用事务
     *
     * @param message      消息正文(layim接收的完整格式)
     * @param toUname      接收人
     * @param isOfflineMsg 是否离线消息
     */
    @Override
    public void record(SocketMessageEntity message, String toUname, boolean isOfflineMsg) {
        chatRecordService.record(message, toUname, isOfflineMsg);
    }

    /**
     * 获取指定用户的基础 LayimUser信息
     *
     * @param chatUserId im_chat_user表id
     * @return
     */
    @Override
    public LayImUser getLayimMine(Long chatUserId) {
        return chatUserService.getLayimMine(chatUserId);
    }

    /**
     * 获取指定用户的基础 LayimUser信息
     *
     * @param uname au_user表username websocket用户唯一标识
     * @return
     */
    @Override
    public LayImUser getLayimMine(String uname) {
        return chatUserService.getLayimMine(uname);
    }

    /**
     * 查询指定接收人的所有离线消息
     *
     * @param uname
     * @return
     */
    @Override
    public List<SocketMessageEntity> offlineRecord(String uname) {
        return chatRecordService.offlineRecord(uname);
    }

    /**
     * 获取指定标段的群聊成员列表
     * 改方法需要根据标段ID查询
     * 查询的群列表成员为该标段的所有已报名的投标人的列表合集
     *
     * @param sectionId
     * @return
     */
    public List<User> listGroupMembers(Long sectionId) {
        if (sectionId == null) {
            return null;
        }

        // 指定标段下的所有投标人递交列表
        BidSectionTenderDTO bidSectionTender = new BidSectionTenderDTO();
        bidSectionTender.setSectionId(sectionId);
        bidSectionTender.setStatus(1);
        Message<List<BidSectionTenderDTO>> listMessage = sectionClient.listBidSectionTender(bidSectionTender);
        if (listMessage.isFail()) {
            return null;
        }
        List<BidSectionTenderDTO> data = listMessage.getData();

        // 查询用户实体信息
        List<Long> bidderIds = CollUtil.map(data, BidSectionTenderDTO::getBidderId, true);
        Message<List<User>> usersMsg = userClient.listUserByIds(bidderIds);
        if (usersMsg.isSuccess()) {
            return usersMsg.getData();
        }
        return null;
    }

    /**
     * 获取指定标段的代理机构用户信息
     * 改方法需要根据标段ID查询
     *
     * @param sectionId
     * @return
     */
    public User getAgencyUser(Long sectionId) {
        if (sectionId == null) {
            return null;
        }
        // 查询该标段的代理机构用户信息
        Message<ProjectBaseInfo> baseInfo = sectionClient.projectBaseInfo(sectionId);
        ProjectBaseInfo baseInfoData = baseInfo.getData();
        Long agencyId = baseInfoData.getUserId();

        Message<User> agencyUser = userClient.getByUserId(agencyId);
        if (agencyUser.isSuccess()) {
            return agencyUser.getData();
        }
        return null;
    }
}
